﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;
using AgvDemo;
//using Maticsoft.DBUtility;//Please add references
namespace AgvDemo.DAL
{
    /// <summary>
    /// 数据访问类:Fault_Record
    /// </summary>
    public partial class Fault_Record
    {
        public Fault_Record()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQLite.GetMaxID("agv_num", "Fault_Record");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int agv_num)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Fault_Record");
            strSql.Append(" where agv_num=@agv_num ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@agv_num", DbType.Int32,4)         };
            parameters[0].Value = agv_num;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(AgvDemo.Model.Fault_Record model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Fault_Record(");
            strSql.Append("agv_num,fault_code,occur_station,occur_time)");
            strSql.Append(" values (");
            strSql.Append("@agv_num,@fault_code,@occur_station,@occur_time)");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@agv_num", DbType.Int32,4),
                    new SQLiteParameter("@fault_code", DbType.Int32,4),
                    new SQLiteParameter("@occur_station", DbType.Int32,4),
                    new SQLiteParameter("@occur_time", DbType.DateTime)};
            parameters[0].Value = model.agv_num;
            parameters[1].Value = model.fault_code;
            parameters[2].Value = model.occur_station;
            parameters[3].Value = model.occur_time;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(AgvDemo.Model.Fault_Record model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Fault_Record set ");
            strSql.Append("fault_code=@fault_code,");
            strSql.Append("occur_station=@occur_station,");
            strSql.Append("occur_time=@occur_time");
            strSql.Append(" where agv_num=@agv_num ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@fault_code", DbType.Int32,4),
                    new SQLiteParameter("@occur_station", DbType.Int32,4),
                    new SQLiteParameter("@occur_time", DbType.DateTime),
                    new SQLiteParameter("@agv_num", DbType.Int32,4)};
            parameters[0].Value = model.fault_code;
            parameters[1].Value = model.occur_station;
            parameters[2].Value = model.occur_time;
            parameters[3].Value = model.agv_num;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int agv_num)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Fault_Record ");
            strSql.Append(" where agv_num=@agv_num ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@agv_num", DbType.Int32,4)         };
            parameters[0].Value = agv_num;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string agv_numlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Fault_Record ");
            strSql.Append(" where agv_num in (" + agv_numlist + ")  ");
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public AgvDemo.Model.Fault_Record GetModel(int agv_num)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select agv_num,fault_code,occur_station,occur_time from Fault_Record ");
            strSql.Append(" where agv_num=@agv_num ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@agv_num", DbType.Int32,4)         };
            parameters[0].Value = agv_num;

            AgvDemo.Model.Fault_Record model = new AgvDemo.Model.Fault_Record();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public AgvDemo.Model.Fault_Record DataRowToModel(DataRow row)
        {
            AgvDemo.Model.Fault_Record model = new AgvDemo.Model.Fault_Record();
            if (row != null)
            {
                if (row["agv_num"] != null && row["agv_num"].ToString() != "")
                {
                    model.agv_num = int.Parse(row["agv_num"].ToString());
                }
                if (row["fault_code"] != null && row["fault_code"].ToString() != "")
                {
                    model.fault_code = int.Parse(row["fault_code"].ToString());
                }
                if (row["occur_station"] != null && row["occur_station"].ToString() != "")
                {
                    model.occur_station = int.Parse(row["occur_station"].ToString());
                }
                if (row["occur_time"] != null && row["occur_time"].ToString() != "")
                {
                    model.occur_time = DateTime.Parse(row["occur_time"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select agv_num,fault_code,occur_station,occur_time ");
            strSql.Append(" FROM Fault_Record ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Fault_Record ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQLite.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.agv_num desc");
            }
            strSql.Append(")AS Row, T.*  from Fault_Record T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "Fault_Record";
			parameters[1].Value = "agv_num";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
